package xx.iverson.service.impl;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import xx.iverson.dto.add.SyUserAddDto;
import xx.iverson.dto.request.SyUserRequestDto;
import xx.iverson.dto.response.SyUserResponseDto;
import xx.iverson.enums.EnumException;
import xx.iverson.mapper.SyUserMapper;
import xx.iverson.result.ReturnResult;
import xx.iverson.service.SyAuthService;
import xx.iverson.service.SyUserService;
import xx.iverson.util.SequenceGenUtil;


/**
 * xx.iverson.service.impl.SyUserServiceImpl
 * <p>
 * on 2025/6/18-下午3:03
 *
 * @author weijx
 */
@Service
@Slf4j
public class SyUserServiceImpl implements SyUserService {
    @Resource
    private SyUserMapper userMapper;
    @Resource
    private SyAuthService authService;

    @Override
    public ReturnResult<?> register(SyUserAddDto user) {
        SyUserRequestDto requestDto = new SyUserRequestDto(user.getUsername(), user.getPhone(), user.getPassword());
        // 检查用户名是否已存在
        SyUserResponseDto existingUser = userMapper.selectByUsername(requestDto);
        if (existingUser != null) {
            return ReturnResult.error(EnumException.USER_REGISTER_ERROR.getErrorCode(),
                    EnumException.USER_REGISTER_ERROR.getErrorMsg(), null);
        }
        // 密码加密
        String encryptedPassword = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(encryptedPassword);
        // 设置ID和时间
        user.setId(SequenceGenUtil.getKey());
        // 保存用户
        if (user.getNickname() == null || user.getNickname().isEmpty()) {
            user.setNickname(SequenceGenUtil.generateRandomString());
        }
        int result = userMapper.registerUser(user);
        if (result <= 0) {
            return ReturnResult.error(EnumException.USER_REGISTER_USER_EXIST.getErrorCode(),
                    EnumException.USER_REGISTER_USER_EXIST.getErrorMsg(), null);
        }

        return ReturnResult.success("注册成功", user);
    }

    @Override
    public ReturnResult<?> login(SyUserRequestDto requestUser) {
        SyUserResponseDto user = userMapper.selectByUsername(requestUser);
        if (user == null) {
            return ReturnResult.error(EnumException.USER_NOT_EXIST.getErrorCode()
                    , EnumException.USER_NOT_EXIST.getErrorMsg(), null);
        }
        // 验证密码
        String encryptedPassword = DigestUtils.md5DigestAsHex(requestUser.getPassword().getBytes());
        if (!user.getPassword().equals(encryptedPassword)) {
            return ReturnResult.error(EnumException.USER_PASSWORD_ERROR.getErrorCode()
                    , EnumException.USER_PASSWORD_ERROR.getErrorMsg(), null);
        }

        //  生成 Token 并存入 Redis
        String token = (String) authService.generateAndStoreToken(requestUser).getData();

        return ReturnResult.success("登录成功", token);
    }

    @Override
    public ReturnResult<?> getUserByUsername(SyUserRequestDto requestDto) {
        return ReturnResult.success("查询成功", userMapper.selectByUsername(requestDto));
    }
}
